<template>
  <view class="container">
    <scroll-view scroll-y="true" style="width: 100%; height: 100vh" v-if="typelist.length != 0">
      <image src="../../../static/img/databankbgc.png" mode="aspectFill" style="width: 100%; height: 340rpx"></image>
      <view class="title" :style="[top]">资料库</view>

      <swiper :autoplay="true" :interval="3000" :duration="1000" class="banner">
        <swiper-item class="banneritem" v-for="item in swiperlist" :key="item.id" @click="goarticle(item.content)">
          <image :src="imgUrl + item.url" mode="aspectFill"></image>
        </swiper-item>
      </swiper>
      <!-- 选择 -->
      <view class="choose">
        <view style="text-align: center" @click="gojtjy(item.id, index)" v-for="(item, index) in typelist" :key="item.id">
          <image :src="imgUrl + item.image" mode="aspectFill" style="width: 100rpx; height: 100rpx"></image>
          <view style="font-size: 26rpx; font-family: PingFang SC, PingFang SC; font-weight: 500; color: #222222">{{ item.name }}</view>
        </view>
      </view>
      <!-- 文章 -->
      <scroll-view scroll-y="true" style="width: 100%; height: 900rpx; margin-top: 40rpx" @scrolltolower="tolower">
        <view class="articlebox" v-for="item in datalist" :key="item.id" @click="godetail(item.id)">
          <image :src="imgUrl + item.pic" mode="aspectFill" style="width: 710rpx; height: 280rpx"></image>
          <view style="font-size: 28rpx; font-family: PingFang SC, PingFang SC; font-weight: bold; color: #222222; padding-left: 30rpx; margin-top: 10rpx">
            {{ item.title }}
          </view>
        </view>
      </scroll-view>
    </scroll-view>
    <view style="width: 750rpx; height: 100rpx"></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isiphone: false,
      top: {
        paddingTop: '0rpx'
      },
      page: 1,
      imgUrl: '',
      typelist: [], // 分类列表
      datalist: [], // 资料列表
      swiperlist: [] //轮播列表
    };
  },
  methods: {
    // 跳转轮播图详情
    goarticle(txt) {
      uni.navigateTo({
        url: '/pages/swiperdetail/swiperdetail?txt=' + encodeURIComponent(txt)
      });
    },
    // 获取资料库的轮播图
    async getswiperlist() {
      const res = await uni.$http.post('/news/banner', {
        type: 'student'
      });
      // console.log('资料库的轮播图', res.data);
      this.swiperlist = res.data.data.list;
    },
    // 获取分类列表
    async gettypelist() {
      const res = await uni.$http.post('/news/category', {
        type: 'student'
      });
      // console.log('资料库的分类列表', res);
      this.typelist = res.data.data.list;
    },
    // 获取资料列表
    async getziliaolist() {
      const res = await uni.$http.post('/news/articleList', {
        page: this.page,
        type: 'student',
        category_id: '3'
      });
      console.log('获取的资料列表--', res.data.data.list);
      if (res.data.data.list.length == 0) {
        return uni.showToast({
          title: '数据已加载完毕',
          icon: 'none'
        });
      } else {
        this.datalist.push(...res.data.data.list);
      }
    },
    //上拉触底
    tolower() {
      this.page++;
      this.getziliaolist();
      console.log(this.page);
    },
    // 点击类目跳转
    gojtjy(id, index) {
      if (index == 0) {
        // 跳转到教学咨询
        uni.navigateTo({
          url: '/parents/jxzx/jxzx?id=' + id
        });
      } else if (index == 1) {
        // 跳转到学习方法
        uni.navigateTo({
          url: '/parents/xxff/xxff?id=' + id
        });
      } else if (index == 2) {
        // 跳转到家庭教育
        uni.navigateTo({
          url: '/parents/jtjy/jtjy?id=' + id
        });
      } else if (index == 3) {
        // 跳转到资料下载
        uni.navigateTo({
          url: '/parents/zlxz/zlxz?id=' + id
        });
      }
    },
    // 跳转到文章详情
    godetail(id) {
      uni.navigateTo({
        url: '/parents/articledetail/articledetail?id=' + id
      });
    }
  },
  created() {
    // 获取资料库的轮播图
    this.getswiperlist();
    // 获取分类列表
    this.gettypelist();
    // 获取资料列表
    this.getziliaolist();
    // 配置图片的基地址
    this.imgUrl = this.imgurl;
    this.page = 1;
    // 适配苹果的刘海
    const app = getApp();
    this.isiphone = app.$vm.isIPhone;
    if (this.isiphone) {
      this.top.paddingTop = app.$vm.liuhai + 'rpx';
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  box-sizing: border-box;
  .title {
    position: absolute;
    top: 65rpx;
    left: 315rpx;
    font-size: 32rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: bold;
    color: #ffffff;
  }
  .banner {
    position: absolute;
    top: 200rpx;
    left: 20rpx;
    width: 710rpx;
    height: 300rpx;
    .banneritem {
      width: 710rpx;
      height: 300rpx;
      image {
        width: 100%;
        height: 300rpx;
        border-radius: 20rpx;
      }
    }
  }
  .choose {
    display: flex;
    justify-content: space-around;
    margin-top: 200rpx;
  }
  .articlebox {
    box-sizing: border-box;
    width: 710rpx;
    height: 360rpx;
    margin: 30rpx 20rpx;
    background: #ffffff;
    box-shadow: 0rpx 4rpx 4rpx 2rpx rgba(191, 191, 191, 0.16);
    border-radius: 20rpx 20rpx 20rpx 20rpx;
  }
}
</style>
<style>
page {
  background-color: #f8f8f8;
}
</style>
